import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:xvideos/hj_page/home_business/search_page/view/search_hot_table_logic.dart';
import 'package:xvideos/tools_base/refresh/pull_refresh.dart';

import '../../../../tools_base/loading/l_error_widget.dart';
import '../../../../tools_base/loading/loading_center_widget.dart';

class SearchHotTableView<T extends SearchHotTableLogic> extends StatelessWidget {
  final int type;
  const SearchHotTableView({super.key, this.type = 0});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<T>(
      init: instanceController<T>(type),
      global: false,
      dispose: (state) => state.controller?.disposeCustomController(),
      builder: (logic) {
        return pullYsRefresh(
          onInit: (ctr) => logic.controller = ctr,
          onRefresh: (refreshController) => logic.fetchPageData(),
          onLoading: (refreshController) => logic.fetchPageData(isRefresh: false),
          child: () {
            if (logic.isLoading) return LoadingCenterWidget();
            if (logic.dataArr.isEmpty) return CErrorWidget();
            return GridView.builder(
              padding: EdgeInsets.symmetric(horizontal: 10.w),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: logic.crossAxisCount,
                  mainAxisSpacing: logic.spaceV,
                  childAspectRatio: logic.childRatio,
                  crossAxisSpacing: logic.spaceH),
              itemCount: logic.dataArr.length,
              itemBuilder: (BuildContext context, int index) {
                return logic.instanceChildItem(index);
              },
            );
          }(),
        );
      },
    );
  }
}
